package com.wenhao.meituan.interview;

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * @author Wenhao Tong
 * @create 2021-08-08 10:24
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0;i < n;i++) {
            array[i] = scanner.nextInt();
        }
        TreeSet<Integer> set = new TreeSet<>();
        int[] pre = new int[n];
        for (int i = 0;i < n;i++) {
            Iterator<Integer> iterator = set.iterator();
            int x = 0;
            while (iterator.hasNext()) {
                int temp = iterator.next();
                if (array[i] <= temp) {
                    pre[i] = x;
                    break;
                }
                x = temp;
            }
            set.add(array[i]);
        }
        int result = 0;
        for (int i = 0;i < n;i++) {
            result += (i + 1) * pre[i];
        }
        System.out.println(result);
    }
}
